#pragma once
#include "../../muduo/tcp_server.hpp"

//回显服务器
class EchoServer
{
public:
    EchoServer(int port):_server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetClosedCallback(OnClosed);
        _server.SetConnectedCallback(OnConnected);
        _server.SetMessageCallback(OnMessage);
    }

    void Start()
    {
        _server.Start();
    }

private:
    static void OnConnected(const Muduo::Connection::PtrConnection & conn)
    {
        DLOG("new connection:%p",conn.get());
    }

    static void OnClosed(const Muduo::Connection::PtrConnection & conn)
    {
        DLOG("close connection:%p",conn.get());
    }

    static void OnMessage(const Muduo::Connection::PtrConnection & conn,Muduo::Buffer * buf)
    {
        std::cout<<buf->ReadPosition()<<std::endl;
        conn->Send(buf->ReadPosition(),buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        // conn->Shutdown();
    }

private:
    Muduo::TcpServer _server;
};